#include <fcntl.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>

#define  MAXBUF 		4096	
#define  MAX_LEN_PATHNAME  1000
#define  PC_DIR 	"/home"
#define  USB_DIR 	"/media/2C3D-C971"

char path_pc[MAX_LEN_PATHNAME] = {'\0'};
char path_usb[MAX_LEN_PATHNAME] = {'\0'};
char pathname_suffix[100] = {'\0'};


void err_quit ( char* fmt, ...)
{
	perror( fmt );
	exit(-1);
}

void update_path( char* old_pathname, char* suffix, char* pathname )
{

	pathname = strcpy( pathname, old_pathname );
	pathname = strcat( pathname, "/");
	pathname = strcat( pathname, suffix);
}

void handle_file( char* pc_file_path, char* usb_file_path )
{
	
	struct stat 	pc_buf;
	struct stat 	usb_buf;

	if( stat( pc_file_path, &pc_buf) == -1 ){
		err_quit( " handle file-->stat error" );	
	}

  	printf("Last status change:       %s\n", ctime(&pc_buf.st_ctime));
  	//printf("Last status change:       %ld\n", pc_buf.st_ctime);

}

void copy_it( char* pc_file_path, char* usb_file_path)
{
	int in_fd, out_fd;
	char buf[MAXBUF];
	ssize_t nbytes;
	
	if((in_fd = open( pc_file_path, O_RDONLY )) == -1 ){
		err_quit(" copy_it-->open pc file error\n");
	}
	if((out_fd = open( usb_file_path, O_RDWR|O_CREAT, 0777)) == -1){
		err_quit(" copy_it-->open usb file error\n");
	}
	while(( nbytes = read(in_fd, buf, 1024)) > 0 )
	{
		if( write(out_fd, buf, nbytes) != nbytes){
			err_quit(" copy_it-->write error\n");
		}
	}

	if( nbytes < 0 ){
		err_quit("copy_ti-->read error\n");
	}
	close( in_fd );
	close( out_fd );
	printf(" copy done!\n");
	return ;
}


struct dirent* enter_dir(char *pc_pathname, char* usb_pathname, char* pathname_suffix)
{
	DIR 		*tmp1;
	DIR 		*tmp2;
	struct dirent   *item;
	char* 		pc_path;
	char* 		usb_path;
	struct stat 	pc_buf;
	struct stat 	usb_buf;
	char  tmp_pc[MAX_LEN_PATHNAME] = {'\0'};
	char  tmp_usb[MAX_LEN_PATHNAME] = {'\0'};
//0保存目录
	strcpy( tmp_pc, pc_pathname );
	strcpy( tmp_usb, usb_pathname );

	pc_path = pc_pathname;
	usb_path = usb_pathname;


	if((tmp1 = opendir( pc_path )) == NULL){
		err_quit( pc_path );
	}

	if((access( usb_path, F_OK )) < 0){
		printf(" %s is not exist, so we create a dir!\n", usb_path);
		if((mkdir(usb_path, 0777)) < 0){
			err_quit(" mkdir error ");
		}
	}

	if((tmp2 = opendir( usb_path )) == NULL){
		err_quit( usb_path );
	}


	while( (item = readdir(tmp1)) != NULL ){

	//得到dir中每个项目的名字，包括文件和目录，把文件名存入pathname_suffix变量中。
		pathname_suffix = strcpy( pathname_suffix, item->d_name );
		if( *pathname_suffix == '.' )
			continue;

	//更新pc和usb的文件或目录路径。
		update_path( tmp_pc, pathname_suffix, path_pc );
		pc_path = path_pc;
		printf(" pc_path is %s\n", pc_path);


		update_path( tmp_usb, pathname_suffix, path_usb );
		usb_path = path_usb;
		printf(" usb_path is %s\n", usb_path);

	//判断item是file还是dir。
		if( stat( pc_path, &pc_buf) == -1 ){
			err_quit( "stat error" );	
		}
		
		if( S_ISDIR( pc_buf.st_mode ) ){
			printf(" this is a dir: ");
			printf(" %s\n ", pc_path );

			printf(" close dir now!\n");

			enter_dir( pc_path, usb_path, pathname_suffix );
		}else{
			printf(" this is a file: ");
			printf(" %s\n ", pc_path );
			handle_file( pc_path, usb_path);
			copy_it( pc_path, usb_path);
		}

			printf(" ok!\n");
			path_pc[0]='\0';
			path_usb[0]='\0';
		/*
		if( S_ISREG( pc_buf.st_mode ) ){
			printf(" this is a file: ");
			printf(" %s\n ", pc_path );
			handle_file( pc_path, usb_path);
			copy_it( pc_path, usb_path);
		}
		*/

	}
		if((closedir( tmp1 )) < 0 ){
			err_quit("closedir error!");
		}
		if((closedir( tmp2 )) < 0 ){
			err_quit("closedir error!");
		}
		return item; 	
} 



int main()
{
	struct dirent* a;
	DIR* 	tmp;
	
	a = enter_dir( PC_DIR, USB_DIR, pathname_suffix );
	
	return 0;

//	printf(" suffix is %s\n", pathname_suffix);

}

